package org.tio.http.common;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author tanyaowu 
 * 2018年7月1日 上午9:51:04
 */
public class HeaderValue {

	public static class EnumerableValue {

		private static final Map<String, HeaderValue> map = new HashMap<>();

		public static HeaderValue from(String value) {
			HeaderValue ret = map.get(value);
			if (ret == null) {
				synchronized (map) {
					ret = map.get(value);
					if (ret == null) {
						ret = HeaderValue.from(value);
						map.put(value, ret);
					}
				}
			}
			return ret;
		}

	}

	public static class Connection {
		public static final HeaderValue keep_alive = HeaderValue.from(HttpConst.ResponseHeaderValue.Connection.keep_alive);
		public static final HeaderValue close = HeaderValue.from(HttpConst.ResponseHeaderValue.Connection.close);
		public static final HeaderValue Upgrade = HeaderValue.from(HttpConst.ResponseHeaderValue.Connection.Upgrade);
	}
	
	public static class Upgrade{
		public static final HeaderValue WebSocket = HeaderValue.from(HttpConst.ResponseHeaderValue.Upgrade.WebSocket);
	}

	public static class Keep_Alive {
		public static final HeaderValue TIMEOUT_10_MAX_20 = HeaderValue.from("timeout=10, max=20");
	}
	
	public static class Cache_Control {
		public static final HeaderValue MAX_AGE_60 = HeaderValue.from("max-age:60");
	}

	public interface Server {
		public static final HeaderValue TIO = HeaderValue.from(org.tio.http.common.HttpConst.SERVER_INFO);
	}

	public interface Content_Encoding {
		public static final HeaderValue gzip = HeaderValue.from("gzip");
	}
	
	public interface Tio_From_Cache {
		public static final HeaderValue TRUE = HeaderValue.from("true");
		public static final HeaderValue FALSE = HeaderValue.from("false");
	}
	public interface Tio_Webpack_Used_Cache {
		public static final HeaderValue V_1 = HeaderValue.from("1");
	}
	
	

	public static class Content_Type extends EnumerableValue {
		
	}

	public final String value;

	public final byte[] bytes;

	private HeaderValue(String name) {
		this.value = name;
		this.bytes = name.getBytes();
	}

	private HeaderValue(String name, String charset) throws UnsupportedEncodingException {
		this.value = name;
		this.bytes = name.getBytes(charset);
	}
	
	public static HeaderValue from(String name) {
		return new HeaderValue(name);
	}
	
	public static HeaderValue from(String name, String charset) throws UnsupportedEncodingException {
		return new HeaderValue(name, charset);
	}

	@Override
	public String toString() {
		return value;
	}
}
